###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Review.pm.pl -> Review module                                               #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Review;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";

use strict;

require "Output/Review.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  my $LANGUAGE = &Standard::GetLanguage("Review");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  unless ($SL::QUERY{'RID'}) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'MISSING'}->{'RID'});
  }

  my $Resource = $in{'DB'}->BinarySelect(
    TABLE   => "Resources",
    KEY     => $SL::QUERY{'RID'}
  );
  unless ($Resource) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'INVALID'}->{'RID'});
  }

  my $Category = $in{'DB'}->BinarySelect(
    TABLE   => "Categories",
    KEY     => $Resource->{'CATEGORY'}
  );
  
  if (
    $in{'DB'}->Query(
      TABLE   => "Reviews",
      WHERE   => {
        RID => [$SL::QUERY{'RID'}],
        IP  => [$ENV{'REMOTE_ADDR'}]
      },
      MATCH   => "ALL",
      COUNT   => 1
    )
  ) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'ALREADY-REVIEWED'});
  }

  my %INPUT;
  
  $INPUT{'RESOURCE'} = $Resource;
  $INPUT{'CATEGORY'} = $Category;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Review->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});

  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  my $LANGUAGE = &Standard::GetLanguage("Review");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  unless ($SL::QUERY{'RID'}) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'MISSING'}->{'RID'});
  }

  my $Resource = $in{'DB'}->BinarySelect(
    TABLE   => "Resources",
    KEY     => $SL::QUERY{'RID'}
  );
  unless ($Resource) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'INVALID'}->{'RID'});
  }

  my $Category = $in{'DB'}->BinarySelect(
    TABLE   => "Categories",
    KEY     => $Resource->{'CATEGORY'}
  );
  
  if (
    $in{'DB'}->Query(
      TABLE   => "Reviews",
      WHERE   => {
        RID => [$SL::QUERY{'RID'}],
        IP  => [$ENV{'REMOTE_ADDR'}]
      },
      MATCH   => "ALL",
      COUNT   => 1
    )
  ) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'ALREADY-REVIEWED'});
  }

  my @Fields = (
    { name => "NAME"         , required => 1, size => 512 },
    { name => "REVIEW"       , required => 1              }
  );

  my (%RECORD, %INPUT, @Error);

  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Inserting data...                                                    #

  $RECORD{'RID'}           = $SL::QUERY{'RID'};
  $RECORD{'IP'}            = $ENV{'REMOTE_ADDR'};

  $RECORD{'CREATE_SECOND'} = time;
  $RECORD{'CREATE_DATE'}   = &Standard::ConvertEpochToDate($RECORD{'CREATE_SECOND'});
  $RECORD{'CREATE_TIME'}   = &Standard::ConvertEpochToTime($RECORD{'CREATE_SECOND'});

  $RECORD{'REVIEW'}        = &Standard::HTMLize($RECORD{'REVIEW'}) unless ($GENERAL->{'HTML_IN_REVIEWS'});
  $RECORD{'REVIEW'}        = &Standard::FilterWords($RECORD{'REVIEW'});

  (
    $in{'DB'}->Insert(
      TABLE   => "Reviews",
      VALUES  => \%RECORD
    )
  ) || &Error::Error("SL", MESSAGE => "Error inserting record into database. $in{'DB'}->{'ERROR'}");

  (
    $in{'DB'}->Update(
      TABLE   => "Resources",
      VALUES  => {
        REVIEWS => "++\${REVIEWS}"
      },
      KEY     => $SL::QUERY{'RID'}
    )
  ) || &Error::Error("SL", MESSAGE => "Error updating record in database. $in{'DB'}->{'ERROR'}");

  my %INPUT;
  
  $INPUT{'RECORD'}   = \%RECORD;
  $INPUT{'RESOURCE'} = $Resource;
  $INPUT{'CATEGORY'} = $Category;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Review->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);

  return 1;
}

1;